package com.sino.operation.controller;

import com.sino.operation.autoCompareNode.AutoCompareEntity;
import com.sino.operation.autoCompareNode.AutoCompareService;
import com.sino.operation.service.basic.ReleaseCacheService;
import com.sino.operation.service.basic.WorkFlowService;
import com.sino.operation.service.business.CommonNodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Title: WorkFlowController
 * @Description: 工作流回执接口
 * @Author dengls
 * @date 2021/10/13 16:40
 */
@Slf4j
@RestController
@RequestMapping("flow")
@Api(value = "工作流回执", tags = "工作流回执")
public class WorkFlowController {
    @Autowired
    private WorkFlowService workFlowService;
    @Autowired
    private CommonNodeService commonNodeService;
    @Autowired
    private AutoCompareService autoCompareService;
    @Autowired
    private ReleaseCacheService releaseCacheService;

    @PostMapping("/sendTask")
    @ApiOperation(value = "任务推送接口")
    public String taskHandler(@RequestBody Map<String, String> data) {
        String journalNo = data.get("businessKey");
        String taskId = data.get("taskId");
        String transCode = data.get("tranCode");
        String flowId = data.get("processInstanceId");
        String tellerPostName = data.get("tellerPost");
        try {
            workFlowService.handlerTask(flowId, taskId, journalNo, transCode, tellerPostName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "推送成功";
    }

    /**
     * 两录比对
     *
     * @param map
     * @return
     */
    @PostMapping("TotalEntryCompareState")
    public String TotalEntryCompareState(@RequestBody Map map) {
        log.info("两录比对开始--");
        String journalNo = map.get("businessKey").toString();
        String transCode = map.get("tranCode").toString();
        String result = commonNodeService.compareTwiceRecording(journalNo, transCode);
        log.info("两录比对结果:" + result);
        return result;
    }

    /**
     * 授权规则
     *
     * @param map
     * @return
     */
    @PostMapping("AuthorizationRule")
    public String AuthorizationRule(@RequestBody Map map) {
        log.info("授权规则判断开始--");
        String journalNo = map.get("businessKey").toString();
        String transCode = map.get("tranCode").toString();
        String result = commonNodeService.AuthorizationRule(journalNo, transCode);
        log.info("授权规则判断结果:" + result);
        return result;
    }

    /**
     * 判重校验
     *
     * @param map
     * @return
     */
    @PostMapping("RepeatCheck")
    public String repeatCheck(@RequestBody Map map) {
        log.info("判重校验开始--");
        String journalNo = map.get("businessKey").toString();
        String transCode = map.get("tranCode").toString();
        String result = commonNodeService.repeatJudge(journalNo, transCode);
        log.info("判重校验结果:" + result);
        return result;
    }

    /**
     * 自动节点
     *
     * @param map
     * @return
     */
    @PostMapping("AutoNode")
    public String autoNode(@RequestBody Map<String, String> map) {
        log.info("自动节点开始--");
        String journalNo = map.get("businessKey");
        String transCode = map.get("tranCode");
        String flowId = map.get("processInstanceId");
        AutoCompareEntity result = autoCompareService.getAutoCompareResult(transCode, journalNo, flowId);
        log.info("自动节点结果:" + result);
        return result.toString();
    }


    /**
     * 验印判断
     *
     * @param map
     * @return
     */
    @PostMapping("VerifyJudge")
    public String VerifyJudge(@RequestBody Map map) {
        log.info("验印判断开始--");
        String journalNo = map.get("businessKey").toString();
        String result = commonNodeService.VerifyJudge(journalNo);
        log.info("验印判断结果:" + result);
        return result;
    }

    /**
     * 自动验印发送
     *
     * @param map
     * @return
     */
    @PostMapping("AutoVerifyStart")
    public String AutoVerifyStart(@RequestBody Map map) {
        log.info("自动验印发送开始--");
        String journalNo = map.get("businessKey").toString();
        String result = commonNodeService.AutoVerifyStart(journalNo);
        log.info("自动验印发送结果:" + result);
        return result;
    }

    /**
     * 自动验印发送
     *
     * @param map
     * @return
     */
    @PostMapping("AutoVerifyResult")
    public String AutoVerifyResult(@RequestBody Map map) {
        log.info("自动验印结果处理开始--");
        String journalNo = map.get("businessKey").toString();
        String result = commonNodeService.autoVerifyResult(journalNo);
        log.info("自动验印结果处理结果:" + result);
        return result;
    }

    /**
     * 记账开始回执
     *
     * @param map
     * @return
     */
    @PostMapping("ChargeHandle")
    public String ChargeHandle(@RequestBody Map map) throws Exception {
        log.info("记账开始--");
        String journalNo = map.get("businessKey").toString();
        String tranCode = map.get("tranCode").toString();
        String result = commonNodeService.ChargeHandle(journalNo,tranCode);
        if ("0".equals(result)) {
            //记账成功，释放缓存数据
            releaseCacheService.release(journalNo);
        }
        log.info("记账结束");
        return "记账成功";
    }


}
